package com.xydtech.ecard.config.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardBusiReportOther;
import com.xydtech.ecard.config.mapper.EcardBusiAndConfigMapper;
import com.xydtech.ecard.config.mapper.EcardBusiReportOtherMapper;
import com.xydtech.ecard.config.service.IEcardBusiReportOtherService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 电子名片业务填报其他业务类信息Service业务层处理
 *
 * @author xydtech
 * @date 2024-09-30
 */
@Service
public class EcardBusiReportOtherServiceImpl implements IEcardBusiReportOtherService
{
    @Autowired
    private EcardBusiReportOtherMapper ecardBusiReportOtherMapper;
    @Autowired
    private EcardBusiAndConfigMapper ecardBusiAndConfigMapper;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询电子名片业务填报其他业务类信息
     *
     * @param id 电子名片业务填报其他业务类信息主键
     * @return 电子名片业务填报其他业务类信息
     */
    @Override
    public EcardBusiReportOther selectEcardBusiReportOtherById(Long id, String salt)
    {
        EcardBusiReportOther busiReportOther = ecardBusiReportOtherMapper.selectEcardBusiReportOtherById(id);
        if (busiReportOther != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportOther.getOthCustPhone())) {
                    busiReportOther.setOthCustPhone(AESEncryptionUtils.decrypt(busiReportOther.getOthCustPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(busiReportOther.getOthCustIdcard())) {
                    busiReportOther.setOthCustIdcard(AESEncryptionUtils.decrypt(busiReportOther.getOthCustIdcard(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return busiReportOther;
    }

    /**
     * 查询电子名片业务填报其他业务类信息列表
     *
     * @param ecardBusiReportOther 电子名片业务填报其他业务类信息
     * @return 电子名片业务填报其他业务类信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EcardBusiReportOther> selectEcardBusiReportOtherList(EcardBusiReportOther ecardBusiReportOther, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustPhone())) {
                ecardBusiReportOther.setOthCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustIdcard())) {
                ecardBusiReportOther.setOthCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<EcardBusiReportOther> ecardBusiReportOthers = ecardBusiReportOtherMapper.selectEcardBusiReportOtherList(ecardBusiReportOther);
        if (ecardBusiReportOthers.size() > 0) {
            for (EcardBusiReportOther busiReportOther : ecardBusiReportOthers) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReportOther.getOthCustPhone())) {
                        busiReportOther.setOthCustPhone(AESEncryptionUtils.decrypt(busiReportOther.getOthCustPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReportOther.getOthCustIdcard())) {
                        busiReportOther.setOthCustIdcard(AESEncryptionUtils.decrypt(busiReportOther.getOthCustIdcard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

            }
        }
        return ecardBusiReportOthers;
    }

    /**
     * 新增电子名片业务填报其他业务类信息
     *
     * @param ecardBusiReportOther 电子名片业务填报其他业务类信息
     * @return 结果
     */
    @Override
    public int insertEcardBusiReportOther(EcardBusiReportOther ecardBusiReportOther, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustPhone())) {
                ecardBusiReportOther.setOthCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustIdcard())) {
                ecardBusiReportOther.setOthCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportOtherMapper.insertEcardBusiReportOther(ecardBusiReportOther);
    }

    /**
     * 修改电子名片业务填报其他业务类信息
     *
     * @param ecardBusiReportOther 电子名片业务填报其他业务类信息
     * @return 结果
     */
    @Override
    public int updateEcardBusiReportOther(EcardBusiReportOther ecardBusiReportOther, String salt)
    {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustPhone())) {
                ecardBusiReportOther.setOthCustPhone(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReportOther.getOthCustIdcard())) {
                ecardBusiReportOther.setOthCustIdcard(AESEncryptionUtils.encrypt(ecardBusiReportOther.getOthCustIdcard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardBusiReportOtherMapper.updateEcardBusiReportOther(ecardBusiReportOther);
    }

    /**
     * 批量删除电子名片业务填报其他业务类信息
     *
     * @param ids 需要删除的电子名片业务填报其他业务类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportOtherByIds(String ids)
    {
        //循环删除电子名片业务填报信息和业务类型关联表
        for (String busiId : ids.split(",")) {
            ecardBusiAndConfigMapper.deleteEcardBusiAndConfigByConfigIdAndBusiType(busiId, "4");
        }
        return ecardBusiReportOtherMapper.deleteEcardBusiReportOtherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片业务填报其他业务类信息信息
     *
     * @param id 电子名片业务填报其他业务类信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardBusiReportOtherById(Long id)
    {
        return ecardBusiReportOtherMapper.deleteEcardBusiReportOtherById(id);
    }

    /**
     * 导出电子名片业务填报其他业务类信息列表
     * @param ecardBusiReportOther
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LinkedHashMap<String, Object>> selectEcardBusiReportOtherListByExport(EcardBusiReportOther ecardBusiReportOther, String salt) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardBusiReportOther.setDictDeptId(sysDept.getDeptId());
        List<LinkedHashMap<String, Object>> ecardBusiReportOthers = ecardBusiReportOtherMapper.selectEcardBusiReportOtherListByExport(ecardBusiReportOther);
        List<LinkedHashMap<String, Object>> ecardBusiReportOtherMaps = new ArrayList<>();
        for (LinkedHashMap<String, Object> busiReportOtherMap : ecardBusiReportOthers) {
            LinkedHashMap<String, Object> maps = new LinkedHashMap<>();
            maps.put("客户姓名", busiReportOtherMap.get("oth_cust_name"));
            maps.put("业务名称", busiReportOtherMap.get("busi_name"));
            maps.put("积分", busiReportOtherMap.get("integral_nums"));
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReportOtherMap.get("oth_cust_phone"))) {
                    maps.put("联系方式", AESEncryptionUtils.decrypt(String.valueOf(busiReportOtherMap.get("oth_cust_phone")), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(busiReportOtherMap.get("oth_cust_idcard"))) {
                    maps.put("身份证号", AESEncryptionUtils.decrypt(String.valueOf(busiReportOtherMap.get("oth_cust_idcard")), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            maps.put("产品类型", busiReportOtherMap.get("oth_prd_type"));
            maps.put("金额", busiReportOtherMap.get("oth_amount"));
            maps.put("营销日期", busiReportOtherMap.get("per_marketing_date"));
            maps.put("备注说明", busiReportOtherMap.get("remark"));
            maps.put("创建时间", busiReportOtherMap.get("creation_time"));
            maps.put("营销经理", busiReportOtherMap.get("user_name"));
            maps.put("员工号", busiReportOtherMap.get("user_code"));
            maps.put("归属机构", busiReportOtherMap.get("dept_name"));
            ecardBusiReportOtherMaps.add(maps);
        }
        return ecardBusiReportOtherMaps;
    }
}
